package cn.gudian.cms.Job

class PlaceController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [placeInstanceList: Place.list(params), placeInstanceTotal: Place.count()]
    }

    def create = {
        def placeInstance = new Place()
        placeInstance.properties = params
        return [placeInstance: placeInstance]
    }

    def save = {
        def placeInstance = new Place(params)
        if (!placeInstance.hasErrors() && placeInstance.save()) {
            flash.message = "place.created"
            flash.args = [placeInstance.id]
            flash.defaultMessage = "Place ${placeInstance.id} created"
            redirect(action: "show", id: placeInstance.id)
        }
        else {
            render(view: "create", model: [placeInstance: placeInstance])
        }
    }

    def show = {
        def placeInstance = Place.get(params.id)
        if (!placeInstance) {
            flash.message = "place.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Place not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [placeInstance: placeInstance]
        }
    }

    def edit = {
        def placeInstance = Place.get(params.id)
        if (!placeInstance) {
            flash.message = "place.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Place not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [placeInstance: placeInstance]
        }
    }

    def update = {
        def placeInstance = Place.get(params.id)
        if (placeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (placeInstance.version > version) {
                    
                    placeInstance.errors.rejectValue("version", "place.optimistic.locking.failure", "Another user has updated this Place while you were editing")
                    render(view: "edit", model: [placeInstance: placeInstance])
                    return
                }
            }
            placeInstance.properties = params
            if (!placeInstance.hasErrors() && placeInstance.save()) {
                flash.message = "place.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Place ${params.id} updated"
                redirect(action: "show", id: placeInstance.id)
            }
            else {
                render(view: "edit", model: [placeInstance: placeInstance])
            }
        }
        else {
            flash.message = "place.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Place not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def placeInstance = Place.get(params.id)
        if (placeInstance) {
            try {
                placeInstance.delete()
                flash.message = "place.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Place ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "place.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Place ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "place.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Place not found with id ${params.id}"
            redirect(action: "list")
        }
    }

    def deleteAll = {
     def qstr = params.qstr
     if (qstr) {
       try {
         Place.executeUpdate("delete Place where id in(" + qstr + ")")
         flash.message = "delete successfully:" + qstr
         redirect(action: "list")
       } catch (Exception e) {
         flash.message = "delete failed:" + qstr
         redirect(action: "list")
       }
     }
}
}
